#!/usr/bin/python
import threading
import SocketServer

path = 'test.txt'  #这个是全局变量path

#设置SocketServer的handle

class Myhandler(SocketServer.BaseRequestHandler):
    def setup(self):#类似于构造函数   setup  是不可以接收参数的
        self.file = open(path, 'r')
        self.content = self.file.read()

    def handle(self):
        print '%s:%s is connected' % (self.client_address)
        request_data = self.request.recv(512)
        print request_data
        self.request.sendall(self.content)

    def finish(self):
        print '%s is done ' % self.client_address[0]
        if self.file.closed == False:
            self.file.close()

#通过多继承，形成新的支持多线程的服务器
class MyServer(SocketServer.TCPServer, SocketServer.ThreadingMixIn):
    pass

#实例化服务器     绑定地址和写好的Handler 做为参数传入
m = MyServer(('', 8000), Myhandler)
#启动多线程
th = threading.Thread(target=m.serve_forever,args=())
th.start()
m.shutdown()
m.server.close()